﻿using System;
using System.Collections.Specialized;
using System.Configuration;

namespace rpxnow_library.rpxnowapi.conf
{
    /// <summary>
    /// This class reads the configuration keys from App.config.
    /// </summary>
    internal sealed class ApplicationConfiguration : IConfigurationSectionHandler
    {
        /// <summary>
        /// Static constructor for the object.
        /// </summary>
        static ApplicationConfiguration()
        {
            Object obj = ConfigurationManager.GetSection("rpxnowapi");
        }

        #region IConfigurationSectionHandler Members

        object IConfigurationSectionHandler.Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            NameValueCollection settings;
            try
            {
                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            }
            catch
            {
                settings = null;
            }

            if (settings != null)
            {
                // Read Application settings from configuration file.
                ApplicationConfiguration.apiKey = ApplicationConfiguration.ReadSetting(settings, API_KEY, "");
                ApplicationConfiguration.rootUrl = ApplicationConfiguration.ReadSetting(settings, ROOT_URL, "https://rpxnow.com/api/v2/");
                ApplicationConfiguration.format = ApplicationConfiguration.ReadSetting(settings, FORMAT, "json");
            }
            return null;


        }

        #endregion

        /// <summary>
        /// Reads a setting from a given NameValueCollection, and sets
        /// default value if the key is not available in the collection.
        /// </summary>
        /// <param name="settings">The settings collection from which the keys
        /// are to be read.</param>
        /// <param name="key">Key name to be read.</param>
        /// <param name="defaultValue">Default value for the key.</param>
        /// <returns>Actual value from settings, or defaultValue if settings
        /// does not have this key.</returns>
        private static string ReadSetting(NameValueCollection settings, string key, string defaultValue)
        {
            if (settings == null || key == null)
                throw new ArgumentNullException();

            try
            {
                Object setting = settings[key];
                return (setting == null) ? defaultValue : (string)setting;
            }
            catch
            {
                return defaultValue;
            }
        }

        #region Public Properties

        public static string ApiKey
        {
            get
            {
                return ApplicationConfiguration.apiKey;
            }
        }

        public static string RootUrl
        {
            get
            {
                return ApplicationConfiguration.rootUrl;
            }
        }

        public enum MessageFormat
        {
            Json,
            Xml
        }

        public static MessageFormat Format
        {
            get
            {
                return ApplicationConfiguration.format.Equals("json", StringComparison.InvariantCultureIgnoreCase) ? MessageFormat.Json : MessageFormat.Xml;
            }
        }

        #endregion

        #region Private Property Members

        private static string apiKey;
        private static string rootUrl;
        private static string format;

        #endregion

        #region Setting Constants

        private const string API_KEY = "ApiKey";
        private const string ROOT_URL = "RootUrl";
        private const string FORMAT = "Format";

        #endregion
    }
}
